<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../bootsttrap/css/bootstrap.min.css">
    <script src="../jquery-3.6.0.js"></script>
</head>

<body>
    <div class="container">
        <ol class="breadcrumb">
            <li class="callback"><a href="#">返回首页</a></li>
            <li><a href="#">详情页</a></li>
        </ol>
    </div>
    <div class="container">
        <div class="media content">

        </div>
    </div>
    <script>
        //获取ID
        var temp = new URLSearchParams(window.location.search)
        var id = temp.get('id')
        // var id = sessionStorage.getItem('id')
        console.log(id);
        $.ajax({
            url: "https://api.it120.cc/conner/shop/goods/detail",
            data: {
                id: id
            },
            type: 'get'
        }).then(function b(res) {
            var shujv = res.data.basicInfo
            console.log(shujv)
            var str = ''
            str += `<div class="media-left">
                  <a href="#">
                    <img class="media-object" src="${shujv.pic}" alt="...">
                  </a>
                </div>
                <div class="media-body">
                  <h4 class="media-heading">${shujv.name}</h4>
                  <p><span>价格：${shujv.minPrice}</span></p>
                  <p><span>库存：${shujv.stores}</span></p>
                  <button>加收藏</button>
                  <button class='addgo'>加购物车</button>
                  
                </div>`
            document.querySelector('.content').innerHTML = str
             //给加购添加点击事件
        $('.content').on('click', '.addgo', function () {
            //判断是否登录
            var code = localStorage.getItem('code')
            console.log(code);
            if (code != 0) {
                location.href = './login.html'
            } else {
                var token = localStorage.getItem('token')
                $.ajax({
            type: 'post',
            url: 'https://api.it120.cc/conner/shopping-cart/add',
            data: {
                goodsId: id,
                number: 1,
                token
            }
        }).then(function dd(res) {
            console.log(res)
            console.log(res.data.items);
            // confirm('加入成功，是否跳转购物车')
            if(confirm('加入成功，是否跳转购物车')){
                var id1=shujv.id
                console.log(id1);
                   //跳转购物车页面
                location.href = `./cart.html?id=${id1}`
            }
        })
                // var id1=shujv.id
                // console.log(id1);
                //    //跳转购物车页面
                // location.href = `./cart.html?id=${id1}`

            }

        })
        })
        $('.callback').click(function () {
            location.href = './index.html'
        })
       
    </script>

</body>

</html>